#!/usr/bin/env python
# -*- coding: utf-8 -*

import clutter
from clutter import Stage, Group, Rectangle, Label
from icon import ActionIcon
from buttons import Texture
from infobar import InfoBar
from widgets_area import WidgetsArea
from gstmanager.event import EventListener

import logging, sys

class IconBar(Group):
    def __init__(self, width=800, height=480, title="Default canvas"):
        Group.__init__(self)
        self.stage = stage = Stage()

        self.max_icon_height = 0
        self.icon_bar_content_size = 0
        self.icon_bar_available_size = width 
        self.icon_bar_remaining_size = self.icon_bar_available_size
        self.icon_bar_contents_nb = 0
        self.icon_bar_height = 0

        self.build_background()
        self.show()

    def build_background(self):
        filename = "/usr/share/touchwizard/images/iconbar_bg.png"
        import os
        path=os.path.join(os.path.abspath(os.path.dirname(__file__)),filename)
        self.back = back = Texture(path)
        back.show()
        super(Group, self).add(back)

    def add_icon_raw(self, icon):
        icon.show()
        self.add(icon)
        self.icon_bar_content_size += (icon.get_width())*icon.get_scale()[0]
        self.icon_bar_remaining_size -= icon.get_width()*icon.get_scale()[0]

        self.icon_bar_contents_nb += 1
        self.layout()

    def add_icon(self, **kwargs):
        icon = ActionIcon(**kwargs)
        self.add_icon_raw(icon)

    def remove_by_event(self, event_name):
        for icon in self:
            print icon.__class__
            if icon.launch_evt == event_name:
                self.remove(icon)

    def layout(self):
        if self.icon_bar_content_size > self.icon_bar_available_size:
            downscale_factor = float(self.icon_bar_available_size)/float(self.icon_bar_content_size)
        else:
            downscale_factor = 1

        icon_max = 0
        self.icon_bar_height = 0
        self.icon_bar_remaining_size = self.icon_bar_available_size
        for icon in self.get_children():
            icon_max = max(icon.get_width(), icon_max)
            if not isinstance(icon, Texture):
                icon.set_scale(downscale_factor, downscale_factor)
                self.icon_bar_remaining_size -= icon.get_width()*icon.get_scale()[0]

        border = self.icon_bar_remaining_size/self.icon_bar_contents_nb
        position = 5
        count = 0
        for icon in self.get_children():
            if not isinstance(icon, Texture):
                icon.set_position(int(position), 0)
                position += icon.get_width()+border
                self.icon_bar_height = max(self.icon_bar_height, icon.get_height())
                count += 1
                if count == self.icon_bar_contents_nb -1:
                    position = self.stage.get_width() - icon.get_width() -5
        self.set_position(0, self.stage.get_height()-self.icon_bar_height)
        self.back.set_height(self.icon_bar_height)

    def remove_icon_by_event_name(self, event_name):
        for icon in self.get_children():
            if icon != self.back:
                if icon.launch_evt == event_name:
                    self.remove(icon)
                    self.icon_bar_content_size -= icon.get_width() + 5

if __name__ == "__main__":
    b = IconBar()
    b.add_icon(file="home")
    b.add_icon(file="home")
    b.show()
    s = clutter.Stage()
    s.add(b)
    s.show()
    s.show_all()
    clutter.main()
